package com.iot.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author Cao.ning
 * @date 2023/3/7
 */
public class JSONUtils {

    public static JSONObject transProp(JSONObject origin)
    {
        if(origin != null && !origin.isEmpty())
        {
            for(String key : origin.keySet())
            {
                Object val = origin.get(key);
                if(val != null)
                {
                    if(StringUtils.isNumber(val.toString()))
                    {
                        try{
                            Long l = Long.parseLong(val.toString());
                            origin.put(key, l);
                        }catch (Exception e){
                            try{
                                Double d = Double.parseDouble(val.toString());
                                origin.put(key, d);
                            }catch (Exception e2){}
                        }
                    }
                    else if(StringUtils.isBoolean(val.toString()))
                    {
                        origin.put(key, Boolean.parseBoolean(val.toString()));
                    }
                    else if(StringUtils.isJsonObj(val.toString()))
                    {
                        JSONObject jsonObj = JSONObject.parseObject(val.toString());
                        jsonObj = transProp(jsonObj);
                        origin.put(key, jsonObj);
                    }
                    else if(StringUtils.isJsonArr(val.toString()))
                    {
                        JSONArray jsonArr = JSONArray.parseArray(val.toString());
                        jsonArr = transProp(jsonArr);
                        origin.put(key, jsonArr);
                    }
                }
            }
        }
        return origin;
    }

    public static JSONArray transProp(JSONArray origin)
    {
        if(origin != null && !origin.isEmpty()) {
            for (int i = 0; i < origin.size(); i ++) {
                Object obj = origin.get(i);
                if (obj != null) {
                    if(StringUtils.isNumber(obj.toString()))
                    {
                        try{
                            Long l = Long.parseLong(obj.toString());
                            origin.set(i, l);
                        }catch (Exception e){
                            try{
                                Double d = Double.parseDouble(obj.toString());
                                origin.set(i, d);
                            }catch (Exception e2){}
                        }
                    }
                    else if(StringUtils.isBoolean(obj.toString()))
                    {
                        origin.set(i, Boolean.parseBoolean(obj.toString()));
                    }
                    else if(StringUtils.isJsonObj(obj.toString()))
                    {
                        JSONObject jsonObj = JSONObject.parseObject(obj.toString());
                        jsonObj = transProp(jsonObj);
                        origin.set(i, jsonObj);
                    }
                    else if(StringUtils.isJsonArr(obj.toString()))
                    {
                        JSONArray jsonArr = JSONArray.parseArray(obj.toString());
                        jsonArr = transProp(jsonArr);
                        origin.set(i, jsonArr);
                    }
                }
            }
        }
        return origin;
    }

    public static void main(String[] args) {
        String msg = "{\"wifi\":false,\"signalStrength\":31,\"timeNow\":\"2022-10-09 16:51:07\",\"guard\":true,\"IMEI\":\"869298059564658\",\"etherMac\":\"\",\"wifiAp\":false,\"battery\":100,\"space\":55,\"iccid\":\"898604790420C0394595\",\"tts\":true,\"bluetooth\":false,\"operators\":\"中国移动\",\"vpn\":false,\"signalType\":\"4G\",\"dpis\":160,\"wifiMac\":\"\",\"firmware\":\"LuatOS-Air_V4013_RDA8910_TTS_NOLVGL_FLOAT\",\"board\":\"Air724UG\"}";
        JSONObject obj = transProp(JSONObject.parseObject(msg));
        System.out.println(obj);
    }
}
